package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/utils/BeanUtils"

	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type OperationLogEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOperationLogEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OperationLogEditLogic {
	return &OperationLogEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// OperationLogEdit 更新操作日志
func (l *OperationLogEditLogic) OperationLogEdit(in *property_service.OperationLog) (*property_service.Response, error) {
	// 获取操作日志模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).OperationLog
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、CreatedAt、UpdatedAt
	if _, err := do.UpdateColumns(&model.OperationLog{
		Module:           in.Module,                                 // 操作模块
		ModuleName:       in.ModuleName,                             // 操作模块名
		OperationType:    in.OperationType,                          // 操作类型
		OperationTarget:  in.OperationTarget,                        // 操作对象
		OperationContent: in.OperationContent,                       // 操作内容
		OperationStatus:  in.OperationStatus,                        // 操作结果
		OperatorID:       in.OperatorID,                             // 操作人ID
		OperatorName:     in.OperatorName,                           // 操作人姓名
		OperatorIP:       in.OperatorIP,                             // 操作IP地址
		OperationTime:    BeanUtils.GetDateFormat(in.OperationTime), // 操作时间
		Remark:           in.Remark,                                 // 备注
		UpdatedAtuser:    in.UpdatedAtuser,                          // 更新人
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
